package com.china08.yxyapi.api.home.activity.weike;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.YxyHuodongWeikeContestCatalogue;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyHuodongWeikeContestCatalogueRepository;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;

@CrossOrigin
@RestController
public class ActivityDetailController extends YxyApiBaseController {

    @Value("${yxy.res.url}")
    private String yxy_res_url;

    @Autowired
    private YxyHuodongWeikeContestCatalogueRepository yxyHuodongWeikeContestCatalogueRepository;

    @Autowired
    private YxyResResourceRepository yxyResResourceRepository;

    @RequestMapping(value = "/api_yw/home/activity/tea/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(@RequestBody ReqModel model) {
        RespModel respModel = new RespModel();
        YxyHuodongWeikeContestCatalogue yxy = yxyHuodongWeikeContestCatalogueRepository.findOne(model.id);
        respModel.id = yxy.getId();
        respModel.phaseName = yxy.getPhaseName();
        respModel.subjectName = yxy.getSubjectName();
        respModel.versionName = yxy.getVersionName();
        respModel.semesterName = yxy.getSemesterName();
        respModel.catalogName = yxy.getBookContentName();
        respModel.knowledageName = yxy.getKnowNames();
        respModel.status = yxy.getStatus();
        if (yxy.getStatus()) {
            YxyResResource one = yxyResResourceRepository.findOne(yxy.getWeikeResId());
            respModel.title = one.getTitle();
            respModel.intro = one.getIntro();
            respModel.location = yxy_res_url + "/res/browse/" + yxy.getWeikeResId();
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String id;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String phaseName;
        public String subjectName;
        public String versionName;
        public String semesterName;
        public String catalogName;
        public String knowledageName;
        public Boolean status;
        public String location;
        public String title;
        public String intro;
    }
}
